Ruby 指令碼不僅僅是一連串指令;它具有結構化的內在組成,從靜態檔案轉換為活躍的執行流程。這個生命週期由 解釋器 解析三種精確的表達式類型: 常量值 (固定值), 變數引用以及 方法呼叫。
1. 語法基礎
Ruby 保留特定關鍵字——記錄於 表 22.3 (例如, alias、 class、 yield)——這些關鍵字構成了語言的骨架結構。它們不能用作識別符,確保解析器能區分邏輯與資料。
2. 執行門檻
模組化設計中一個關鍵模式是 if __FILE__ == $0。此判斷可辨識檔案是否為入口點(主腳本)或被當作函式庫載入。透過利用 __FILE__ 與 __LINE__,程式可在檔案系統中保持自我覺察。
3. 嵌入式資料
這 __END__ 標記如同物理上的尾端。解釋器會忽略其後的所有內容,但透過 DATA IO 物件提供資料,讓配置或範本可自包含。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which of the following is NOT one of the three expression types Ruby evaluates?
Literals
Variable references
Macro definitions
Method invocations
✅ Correct!
Ruby strictly evaluates Literals, Variable references, and Method invocations. Macros are not a core expression type in standard Ruby.❌ Incorrect
Review the core concept: Ruby's interpreter handles Literals, Variables, and Methods.QUESTION 2
What is the purpose of the
if __FILE__ == $0 idiom?To check if the file is empty.
To differentiate between running a file directly vs. requiring it as a library.
To reset the object_id of the main thread.
To loop through the SCRIPT_LINES__ array.
✅ Correct!
This logic gate ensures code (like tests or demos) only runs when the script is the primary execution target.❌ Incorrect
It is about execution context: is this the main script or a dependency?QUESTION 3
Which constant provides access to the text placed after the
__END__ token?EOF
TRAILER
DATA
SCRIPT_LINES__
✅ Correct!
The DATA constant is an IO object pointing to the content after the __END__ marker.❌ Incorrect
Check the section on Embedded Data Segments; the answer is a four-letter constant.QUESTION 4
According to Table 22.3, which of these is a Reserved Word?
puts
yield
string
object_id
✅ Correct!
yield is a keyword used for blocks; puts and object_id are methods, and string is a class name.❌ Incorrect
Keywords like yield, alias, and elsif are reserved by the language core.QUESTION 5
What information do
RUBY_VERSION and RUBY_PLATFORM provide?The local variable scope count.
The environmental context of the execution environment.
The memory address of the TOPLEVEL_BINDING.
The number of lines in the current file.
✅ Correct!
These constants define the interpreter's version and the underlying hardware/OS architecture.❌ Incorrect
These are environmental 'magic' constants.The Self-Contained Utility Case
Applying Source Layout Principles
A developer needs to create a Ruby script that acts as both a standalone CLI tool and a reusable module. It must include default configuration values within itself and verify it is running on Ruby 3.0 or higher.
Q
1. How should the developer implement the version check and the 'main' execution logic?
Solution:
Use
Use
abort if RUBY_VERSION < '3.0' at the top. Wrap the CLI logic in if __FILE__ == $0 to prevent it from running when the file is required by other scripts.Q
2. Where should the default configuration be stored for maximum portability without external files?
Solution:
Place the configuration text after an
Place the configuration text after an
__END__ marker at the bottom of the script and read it using the DATA constant (e.g., YAML.load(DATA)).